package com.systop.busafter.task.controller;
import com.systop.busafter.task.service.TaskService;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class TaskController {
    @Autowired
    private TaskService taskService;
    //修改
    @RequestMapping(value="/updatatask",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatatask(@RequestBody Task task){
        int i =  taskService.updatatask(task);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }

    //添加
    // 发布任务 (用户id，出发时间，起点，终点，返程时间，人数，电话号码，订单号，时间）
    // 预约：  (用户id，出发时间，起点，终点，返程时间，人数，电话号码，订单号，时间， 司机id） 修改押金状态
    @RequestMapping(value="/inserttask",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult inserttask(@RequestBody Task task){
        //在99999999和1000000之间任意取一个数字
        int max=99999999,min=1000000;
        int ran2 = (int) (Math.random()*(max-min)+min);
        task.setTask_order_number(ran2);
        task.setTask_state1(0);
        task.setTask_deposit_state(0);
        task.setTask_state(0);
        int i =  taskService.inserttask(task);
        if (i == 0) {
            return ResponseResult.build(500, "添加失败");
        } else {
            return ResponseResult.build(200, "添加成功", i);
        }
    }

    //查看全部
    @RequestMapping(value="/taskListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult taskListall(){
        List<Task> list =  taskService.taskListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过id查
    @RequestMapping(value="/taskIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult taskIdlist(@RequestParam(value = "task_id")int task_id){
        List<Task> list =  taskService.taskIdlist(task_id);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过id查(实体类)
    @RequestMapping(value="/TaskId",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult TaskId(@RequestParam(value = "task_id")int task_id){
       Task task =  taskService.TaskId(task_id);
        if (task == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", task);
        }
    }

    //删除
    @RequestMapping(value="/deletetask",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deletetask(@RequestParam(value = "task_id")int task_id){
       int i =  taskService.deletetask(task_id);
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }


    //通过订单号查看list
    @RequestMapping(value="/taskordernumberList",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult taskordernumberList(@RequestParam(value = "task_order_number")String task_order_number){
        if (task_order_number.equals("")){
            return ResponseResult.build(20, "查看失败");
        }
        List<Task> list =  taskService.taskordernumberList(Integer.parseInt(task_order_number));
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }


    /*
     *1发布（设置时间戳，结束订单）
     * */

    /*
     *2用户是否同意司机的报价(本地存储订单的id，单号，价格，用户电话号码)
     *   id      1        0
     * 用户id  司机状态  结束状态,
     * task_user_id   task_state1  task_state
     * list
     * csh
     * */
    @RequestMapping(value="/Tobeconfirmed",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult Tobeconfirmed(@RequestBody Task task){
        List<Task>  list =  taskService.Tobeconfirmed(task);
        if (list == null) {
            return ResponseResult.build(500, "查询失败");
        } else {
            return ResponseResult.build(200, "查询成功", list);
        }
    }
    /*
     *3  1) 通过id区修改状态押金状态，押金是司机报价的30%；
     *   2) 不同意 ，司机状态，价格手机号，司机id，都改成0
     * */

    /* *
     *4进行中
     * 任务转进行中
     *    id       1       1       0
     * 用户id  押金状态 司机状态  结束状态
     * task_user_id   task_deposit_state  task_state
     * */
    @RequestMapping(value="/usertasktrueing",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult usertasktrueing(@RequestBody Task task){
        List<Task>  list =  taskService.usertasktrueing(task);
        if (list == null) {
            return ResponseResult.build(500, "查询失败");
        } else {
            return ResponseResult.build(200, "查询成功", list);
        }
    }

    /*
     * 1司机查看全部的任务
     * task_state1  int
     * return int
     * csh
     * */
    @RequestMapping(value="/taskdeiverall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult taskdeiverall(@RequestParam(value = "task_state1")int task_state1){
        List<Task>  list =  taskService.taskdeiverall(task_state1);
        if (list == null) {
            return ResponseResult.build(500, "查询失败");
        } else {
            return ResponseResult.build(200, "查询成功", list);
        }
    }

    /*
     * 2选择接单，通过id修改之前，先去通过id查下司机状态是否为0
     *
     * */


    /*3司机任务进行中
     *  id         1          1         0
     *通过司机id  押金状态   司机状态     结束状态
     *
     * */

    @RequestMapping(value="/deivertasktrueing",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deivertasktrueing(@RequestBody Task task){
        List<Task>  list =  taskService.deivertasktrueing(task);
        if (list == null) {
            return ResponseResult.build(500, "查询失败");
        } else {
            return ResponseResult.build(200, "查询成功", list);
        }
    }

    /*
     *4 修改订单结束状态停止任务
     * */


}
